using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

namespace Game.Core
{
    public unsafe partial struct MoveToAttackRangeJob : IJobEntity
    {
        [ReadOnly] public ComponentLookup<LocalTransform> Positions;
        void Execute(in Attackable attackable, ref Movable movable, in LocalTransform position)
        {
            if (Entity.Null != attackable.Target)
            {
                var tarPos = Positions[attackable.Target];
                var diff = tarPos.Position - position.Position; 
                var distance = math.length(diff);
                movable.Velocity = distance > attackable.Detail.AttackRange ? math.normalize(diff) * movable.Detail.MoveSpeed : 0f;
            }
            else
            {
                movable.Velocity = 0f;
            }
        }
    }
}